package model

import (
	"gorm.io/gorm"
)

type Video struct {
	gorm.Model
	UserId       int64  `gorm:"type:int;not null;commit:'用户Id'"`
	Title        string `gorm:"type:varchar(150);not null;commit:'标题'"`
	Dsc          string `gorm:"type:varchar(150);not null;commit:'描述'"`
	UploadUrl    string `gorm:"type:varchar(150);not null;commit:'图片地址'"`
	LikeCount    int64  `gorm:"type:int;default:0;commit:'点赞数量'"`
	CollectCount int64  `gorm:"type:int;default:0;commit:'收藏数量'"`
	CommentCount int64  `gorm:"type:int;default:0;commit:'发布数量'"`
	Hot          int64  `gorm:"type:int;default:0;commit:'热度'"`
	PayCount     int64  `gorm:"type:int;default:0;commit:'观看次数'"`
	IsVip        int64  `gorm:"type:int;default:0;commit:'0-免费,1-收费'"`
	TypeId       int64  `gorm:"type:int;default:0;commit:'类型Id'"`
	TagId        int64  `gorm:"type:int;default:0;commit:'标签Id'"`
	Status       int64  `gorm:"type:int;default:0;commit:'视频状态:1-下架,2-已上线,3-驳回'"`
	MemberStatus int64  `gorm:"type:int;default:0;commit:'会员状态: 0-不是会员,1-是会员'"`
}
type Type struct {
	gorm.Model
	TypeName string `gorm:"type:varchar(150);not null;commit:'分类名称'"`
	Pid      int64  `gorm:"type:int;not null;commit:'父级ID'"`
}

// 分类+视频关联表
type VideoType struct {
	Title        string
	Dsc          string
	TypeName     string
	VideoId      int64
	CollectCount int64
	PayCount     int64
	Hot          int64
	CommentCount int64
}

func (v *Video) VideoType(db *gorm.DB, keyword string, typename string, sort int64, payCount int64, status int64, memberStatus int64) (videolist []VideoType, err error) {
	query := db.Model(&Video{}).
		Select("videos.title,videos.dsc,types.type_name,videos.id,videos.collect_count,videos.pay_count,videos.hot,videos.comment_count").
		Joins("left join types on videos.type_id = types.id")
	if keyword != "" {
		query.Where("videos.title like ?", "%"+keyword+"%")
	} else if typename != "" {
		query.Where("types.type_name like ?", "%"+typename+"%")
	}
	if payCount != 0 {
		query.Where("videos.pay_count= ?", payCount)
	}
	if status != 0 {
		query.Where("videos.status= ?", status)
	}
	if memberStatus != 0 {
		query.Where("videos.member_status= ?", memberStatus)
	}

	if sort != 0 {
		query.Order("videos.hot desc")
	} else {
		query.Order("videos.comment_count desc")
	}
	query.Find(&videolist)
	return

}

type Tag struct {
	gorm.Model
	TagName  string `gorm:"type:varchar(150);not null;commit:'标签名称'"`
	TageType int64  `gorm:"type:int;default:0;not null;commit:'标签类型: 0-区域,1-题材,2-独特属性'"`
}

// 视频添加
func (v *Video) VideoAdd(db *gorm.DB) error {
	err := db.Create(&v).Error
	return err
}

// 视频审查
func (v *Video) VideoUpdate(db *gorm.DB, id int64, status int64) error {
	err := db.Model(Video{}).Where("id=?", id).Update("status", status).Error
	return err
}

// 分类添加
func (t *Type) TypeAdd(db *gorm.DB) error {
	err := db.Create(&t).Error
	return err
}

// 标签添加
func (g *Tag) TagAdd(db *gorm.DB) error {
	err := db.Create(&g).Error
	return err
}
